//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   COUNTRY
// AUTHOR           :   Craig Chun 
// DATE CREATE      :   2007/07/12
// DESCRIPTION      :   a SEARCH ACTION file for the function, COUNTRY
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/07/12   Craig Chun      1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.country.web;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.setup.country.criteria.CountryHdrSrchCriteria;
import com.erp.app.setup.country.pk.CountryHdrPk;
import com.erp.app.setup.country.schema.CountryHdrSchema;
import com.erp.app.setup.country.vo.CountryHdrVo;
import com.erp.app.util.web.WebUtil;
import com.erp.common.web.SearchResultRow;
import com.erp.framework.util.BeanUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;
import com.erp.framework.util.user.UserDelegate;
import com.erp.framework.controller.action.SimpleSearchStyleActionTemplate;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;

/**
* A <strong>Search Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the user search events
* from the web browsers.
*
* @author          Craig Chun 
* @version 1.0     2007/07/12
* @since           FrameWork 1.0
*
*/
public class CountrySearchAction extends SimpleSearchStyleActionTemplate {

	////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS DEFINITION
	////////////////////////////////////////////////////////////////////////////////

	public static final int TAB_IDX_HDR = -1;
	public static final int TAB_IDX_PORT = 0;

	public static final String HDR = "Hdr";
	public static final String PORT = "Port";

	public static final String ACTION_CHANGE_FOLDER_HDR = ACTION_CHANGE_FOLDER + HDR;
	public static final String ACTION_CHANGE_FOLDER_PORT = ACTION_CHANGE_FOLDER + PORT;


	////////////////////////////////////////////////////////////////////////////////
	// GLOBAL VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////
	// LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// END OF LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// OVERRIDES PARENT'S OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////

	//the following method is only to be overwritten by sub-class if intended
	//to set the aut-search on-load to on/off
	protected boolean getLoadOnEnter() {

		//auto-search when load
		return true;
	}

	//the following method is only to be overwritten by sub-class if intended
	//to set the clearing search result sets on-load to on/off
	protected boolean getResetOnEnter() {

		//reset last searched results when load 
		return true;
	}

	protected void beforeEdit(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		CountrySearchForm form = (CountrySearchForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {

			//TODO: Add the following line to ProjectSecurityConstants.java
			//public static final String SP_SHARE_COUNTRY = "SP_SHARE_COUNTRY";
			String companyCode = WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_COUNTRY);
			form.setSrchCompanyCode(companyCode);

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected String getSimpleSrchMenuId() {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_COUNTRY = "MOD_COUNTRY";
		return ProjectConstants.MOD_COUNTRY;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// IMPLEMENTS PARENT'S ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected String getModuleName(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request,HttpServletResponse response) {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_COUNTRY = "MOD_COUNTRY";
		return ProjectConstants.MOD_COUNTRY;
	}

	protected String getFunctionId() {

		//struts-config xxxxx.do
		return "countrySearch.do";
	}

	protected Class getMasterFormClass() {

		//the corresponding form class
		return CountrySearchForm.class;
	}

	protected Class getMasterVOClass() {

		//corresponding master vo class
		return CountryHdrVo.class;
	}

	protected Class getSearchCriteriaClass() {

		//corresponding searching criteria class
		return CountryHdrSrchCriteria.class;
	}

	protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, Object criteria) {

		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);
		CountryHdrSrchCriteria crit = (CountryHdrSrchCriteria) criteria;

		//TODO: Add the following line to ProjectSecurityConstants.java
		//public static final String SP_SHARE_COUNTRY = "SP_SHARE_COUNTRY";
		String companyCode = WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_COUNTRY);

		crit.setSrchCompanyCode(companyCode);
		crit.setSearchResult(true);
	}

	protected String getMasterFormSessionName() {

		return "countrySearchForm";
	}

	protected SimpleBasePk getPk(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		CountrySearchForm form = (CountrySearchForm) formIn;
		CountryHdrPk pk = null;
//		edit the following with appropriate form field names
		pk = new CountryHdrPk(form.getSrchCompanyCode(), form.getEditCountryNo());

		return pk;
	}

	protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
		HttpServletResponse response, ActionErrors errors){

		CountrySearchForm form = (CountrySearchForm) formIn;
		form.setFormType(ProjectConstants.FORM_TYPE_SEARCH);
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

}

